package com.example.Cairodio;

import java.io.ByteArrayOutputStream;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

/**
 * This class takes over uploading an image in a Bitmap format to the Application's Album on Facebook.
 * @author AnwaRowers
 *
 */
public class UploadPhoto  {
Facebook facebook; 
Handler mHandler; 
static Activity mActivity; 
public UploadPhoto(Facebook facebook) {
	this.facebook = facebook; 
	this.mHandler = new Handler();
	mActivity = new Activity();
}

/**
 * performs the Facebook request of posting the the Image to the Application's
 * Photo Album on Facebook.
 * @param bi
 */
public void postImageonWall(Bitmap bi) {

      byte[] data = null;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
      data = baos.toByteArray();

      Bundle params = new Bundle();
      params.putString(Facebook.TOKEN, facebook.getAccessToken());
      params.putString("method", "photos.upload");
      params.putByteArray("picture", data);
      AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
      try{
    	  mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
    	  
      }catch(Error e) {
    	  Toast.makeText(mActivity, mActivity.getString(R.string.error), 1).show();
      }catch(Exception e) {
    	  Toast.makeText(mActivity, mActivity.getString(R.string.exception), 1).show();
    	  
      }

}
 
public class SampleUploadListener extends BaseRequestListener {

    public void onComplete(final String response, final Object state) {
        try {
            // process the response here: (executed in background thread)
//            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
//            final String src = json.getString("src");
//            final String owner = json.getString("owner");
            final String link = json.getString("link");
            
            //TouristicPostCard.currentPhotoLink = json.getString("link");            
//            UploadPhoto.mActivity.runOnUiThread(new Runnable() {
//                public void run() {
//                if(!link.equals("")) {
//                	Toast.makeText(mActivity, "Photo uploaded to App Gallery on Facebook", 1).show();
//                }
//                }
//            }); 
            
//            Log.d("JSON response", src);
//            Log.d("JSON respons", "owner is " + owner);
            Log.d("Link is ", link);
            

        } catch (JSONException e) {
        	
        } catch (FacebookError e) {
        	Toast.makeText(new Activity(),  e.getMessage(), 1).show();
          
        }
    }
    


}
	
}



